<script setup>

import jing from "@/assets/jing.png";
import {onMounted, ref} from "vue";
import router from "@/modules/router.js";
import {homePageQueryArticle} from "@/api/api.js";

console.log(router.currentRoute.value.query);
const userInfo = ref(router.currentRoute.value.query)
const show = ref(false)
const checked = ref(false)
const time = ref("")
const checkedChange = () => {
  pageNo.value = 0
  getHomePageQueryArticle()
}
const formatDate = (date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0");
  const day = String(date.getDate()).padStart(2, "0");
  return `${year}-${month}-${day}`;
};
const onConfirm = (value) => {
  show.value = false;
  time.value = formatDate(value);
  pageNo.value = 0
  getHomePageQueryArticle();
};

onMounted(() => {
  time.value = "选择时间"
})
const refreshing = ref(false)

const loading = ref(false)

const finished = ref(false)

const pageNo = ref(0)
const onRefresh = () => {
  refreshing.value = true;
  loading.value = true;
  pageNo.value = 0
  getHomePageQueryArticle();
};
const getHomePageQueryArticle = () => {
  pageNo.value = pageNo.value + 1
  let query = {}
  if(checked.value){
    query = {
      pageNo: pageNo.value,
      pageSize: 10,
      digested: 1,
      uid: userInfo.value.user_id,
      createTime: time.value == "选择时间" ? undefined : time.value
    }
  }else{
    query = {
      pageNo: pageNo.value,
      pageSize: 10,
      uid: userInfo.value.user_id,
      createTime: time.value == "选择时间" ? undefined : time.value
    }
  }
  homePageQueryArticle(query).then(res => {
    if (pageNo.value == 1) {
      articleList.value = []
      articleList.value = res.result.records
    } else {
      articleList.value.push(...res.result.records)
    }
    if (articleList.value.length == res.result.total) {
      finished.value = true
    }

  }).finally(() => {
    refreshing.value = false
    loading.value = false
  })
}
const minDate = new Date(2010, 0, 1); // 设置最小日期为2010年1月1日
const maxDate = new Date(); // 设置最大日期为当前日期
const articleList = ref([])
</script>

<template>
  <van-calendar
      v-model:show="show"
      :max-date="maxDate"
      :min-date="minDate"
      @confirm="onConfirm"
  />
  <van-sticky>
    <div class="header">
      <van-icon name="arrow-left" size="25" @click="router.back()"/>
      <van-image
          style="margin: 10px"
          round
          width="40"
          height="40"
          :src="userInfo.userHeadimg"
      />
      <van-switch v-model="checked" @change="checkedChange">
        <template #node>
          <div class="icon-wrapper">
            <van-icon
                :name="
              checked
                ? jing
                : ''
            "
            ></van-icon>
          </div>
        </template>
      </van-switch>
    </div>
  </van-sticky>
<!--  <div class="box">-->
<!--    <van-image-->
<!--        style="margin: 10px"-->
<!--        round-->
<!--        width="40"-->
<!--        height="40"-->
<!--        :src="userInfo.userHeadimg"-->
<!--    />-->
<!--    <div>{{userInfo.name}}</div>-->
<!--    <div>{{userInfo.location}}</div>-->
<!--  </div>-->

  <div class="content-card-list">
    <van-pull-refresh v-model="refreshing" @refresh="onRefresh">
      <van-list
          v-model:loading="loading"
          :finished="finished"
          finished-text="没有更多了"
          @load="getHomePageQueryArticle"
      >
        <share-card v-for="(item, index) in articleList" :key="index" :item="item"></share-card>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<style scoped>
.content-card-list {
  background: #f2f5f9;
  max-width: 837px;
  margin:auto;
}
.box  {
  margin: 5px auto 0 auto;
  width: 100%;
  background-color: #ffffff;
  border-radius: 5px;
  height: 100px;
  max-width: 500px;
  font-size: 20px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}
.time {
  width: 75px;
  text-align: center;
}
.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10px;
  border-bottom: 1px solid #cfcfcf;
  background: #ffffff;
  width: 837px;
  margin: auto;
  border-radius: 10px;
}
.iconTime {
  margin-right: 5px;
}

.timeBox {
  width: 140px;
  border: 1px solid #cfcfcf;
  height: 30px;
  display: flex;
  align-items: center;
  justify-content: space-around;
  border-radius: 5px;
  font-size: 14px;
}
</style>
<style>
.icon-wrapper {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 20px;

}
.van-switch--on {
  background: #107459;
}
</style>
